/*
 * Copyright (C) 2008-2009 Google Inc. 
 * Copyright (C) 2013 Winson Wong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Source is base on Android API Source 16  
 */

package myidea.winson.android.cangjieime;

import java.util.Hashtable;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.Xml;
import android.view.inputmethod.EditorInfo;


public class CangjieKeyboard extends Keyboard {

    private Key mEnterKey;
    //private Key mSpaceKey;
    private Key mShiftKey;
    private boolean mCapsLock;

	private Hashtable<Character,Character> mCangjieKeyCodeTable;
	private StringBuilder mReturnCangjieCodeStr = new StringBuilder();
	
	private Drawable mShiftIcon;
	private Drawable mShiftCapsLockIcon;
	
	public int keySubTextSizeAdj;
    
	public CangjieKeyboard(Context context, int xmlLayoutResId) {
        this(context,xmlLayoutResId,0);
    }
	
    public CangjieKeyboard(Context context, int xmlLayoutResId, int keySizeMode) {
        super(context, xmlLayoutResId,0,keySizeMode);
             
    }

	public CangjieKeyboard(Context context, int layoutTemplateResId, 
            CharSequence characters, int columns, int horizontalPadding) {
        super(context, layoutTemplateResId, characters, columns, horizontalPadding);
    }
	
	
	@Override
    protected void loadKeyboardSizeMode(int keySizeMode, float scale) {
    	switch (keySizeMode){
		case -1:
			keyHeightAdj=(int)(-5f * scale + 0.5f);
			keyTextSizeAdj=(int)(-1.9f * scale + 0.5f);
			keySubTextSizeAdj=(int)(-1.4f * scale + 0.5f);
			labelTextSizeAdj=(int)(-1.9f * scale + 0.5f);			
			break;
		case 0:
			keyHeightAdj=0;
			keyTextSizeAdj=0;
			keySubTextSizeAdj=0;
			labelTextSizeAdj=0;
			break;
		case 1:
			keyHeightAdj=(int)(5f * scale + 0.5f);
			keyTextSizeAdj=(int)(1.9f * scale + 0.5f);
			keySubTextSizeAdj=(int)(1.4f * scale + 0.5f);
			labelTextSizeAdj=(int)(1.9f * scale + 0.5f);			
			break;
		case 2:
			keyHeightAdj=(int)(10f * scale + 0.5f);
			keyTextSizeAdj=(int)(3.8f * scale + 0.5f);
			keySubTextSizeAdj=(int)(2.8f * scale + 0.5f);
			labelTextSizeAdj=(int)(3.8f * scale + 0.5f);			
			break;
		case 3:
			keyHeightAdj=(int)(15f * scale + 0.5f);
			keyTextSizeAdj=(int)(5.7f * scale + 0.5f);
			keySubTextSizeAdj=(int)(4.2f * scale + 0.5f);
			labelTextSizeAdj=(int)(3.8f * scale + 0.5f);			
			break;
		}
	}
    
    
    public boolean getCapsLock(){
    	return mCapsLock;
    }
    
    public void setCapsLock(boolean capsLock){
    	if (this.mCapsLock != capsLock){
        	this.mCapsLock = capsLock;
        	if (capsLock){
        		mShiftKey.icon = mShiftCapsLockIcon;
        	}
        	else{
        		mShiftKey.icon = mShiftIcon;
        	}
    	}    	
    }
    
    public String getCangjieCode(String inputStr){
    	char[] inputCharArray = inputStr.toLowerCase().toCharArray();
    	
    	mReturnCangjieCodeStr.setLength(0);
    	
    	for(char c : inputCharArray){
    		mReturnCangjieCodeStr.append(mCangjieKeyCodeTable.get(c));
    	}
    	    	
    	return mReturnCangjieCodeStr.toString();
    }
    
    @Override
    protected Key createKeyFromXml(Resources res, Row parent, int x, int y, 
            XmlResourceParser parser) {
        CangjieKey key = new CangjieKey(res, parent, x, y, parser);
        
        if (key.codes[0] == 10) {
            mEnterKey = key;
        } else if (key.codes[0] == -1) {
        	mShiftKey = key;
        	mShiftIcon = key.icon;
        	TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), 
                    R.styleable.Cangjie_Keyboard_Key);
        	mShiftCapsLockIcon = a.getDrawable(R.styleable.Cangjie_Keyboard_Key_keyShiftCapLockIcon);
        	a.recycle();
        	/*
        	mShiftKey = new CangjieShiftKey(res, parent, x, y, parser);
        	key = (CangjieKey) mShiftKey;
        	*/
        } /*else if (key.codes[0] == ' ') {
            mSpaceKey = key;
        }*/
        
        
        if (mCangjieKeyCodeTable == null){
        	mCangjieKeyCodeTable = new Hashtable<Character,Character>();
        }
        
        if (key.codes[0] >= 97 && key.codes[0] <= 122){
        	
        	mCangjieKeyCodeTable.put((char) key.codes[0], key.label.charAt(0));	
        }
        
        
        return key;
    }
   
    /**
     * This looks at the ime options given by the current editor, to set the
     * appropriate label on the keyboard's enter key (if it has one).
     */
    
    void setImeOptions(Resources res, int options) {
        if (mEnterKey == null) {
            return;
        }
        
        switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
            case EditorInfo.IME_ACTION_GO:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_go_key);
                break;
            case EditorInfo.IME_ACTION_NEXT:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_next_key);
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                mEnterKey.iconPreview = res.getDrawable(R.drawable.sym_keyboard_search_preview_icon);
                mEnterKey.iconPreview.setBounds(0, 0, mEnterKey.iconPreview.getIntrinsicWidth(), 
                		mEnterKey.iconPreview.getIntrinsicHeight());
                mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
                mEnterKey.label = null;
                break;
            case EditorInfo.IME_ACTION_SEND:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_send_key);
                break;
            case EditorInfo.IME_ACTION_DONE:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_done_key);
                break;
            default:
                mEnterKey.iconPreview = res.getDrawable(R.drawable.sym_keyboard_return_preview_icon);
                mEnterKey.iconPreview.setBounds(0, 0, mEnterKey.iconPreview.getIntrinsicWidth(), 
                		mEnterKey.iconPreview.getIntrinsicHeight());
                mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
                mEnterKey.label = null;
                break;
        }
        
    }
    

    /*
    void setSpaceIcon(final Drawable icon) {
        if (mSpaceKey != null) {
            mSpaceKey.icon = icon;
        }
    }
	*/
}
